home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / cenviw1.zip / INSTALL.CMM < prev    next >
Text File  |  1993-08-09  |  11KB  |  303 lines

  1. /*************************************************************************
  2.  ***                            Install.cmm                            ***
  3.  *** This is the installation program to set up CEnvi unregistered     ***
  4.  *** shareware on your computer.  Before running this program, CEnvi   ***
  5.  *** must be copied to a hard disk directory.  This Install.cmm        ***
  6.  *** program is identical for DOS, OS/2, and Windows version of CEnvi  ***
  7.  *** and this program is itself an example for how to use CEnvi        ***
  8.  *************************************************************************/
  9.  
  10.  
  11. main(argc,argv)
  12. {
  13.    // There should be no input args.  If there are then the user needs help.
  14.    ScreenClear();
  15.    if ( argc != 1 )
  16.       GiveInstructionsAndExit();
  17.  
  18.    // Get the current directory.  The GetInstallDirectory() function assures
  19.    // that it is not a floppy.
  20.    InstallDirectory = GetInstallDirectory(argv[0]);
  21.  
  22.    if defined( _DOS_ )
  23.       DosOrOS2Install(InstallDirectory,"C:\\AUTOEXEC.BAT","C:\\AUTOEXEC.BAK");
  24.    else if defined( _OS2_)
  25.       DosOrOS2Install(InstallDirectory,"C:\\CONFIG.SYS","C:\\CONFIG.BAK");
  26.    else {
  27.       assert( defined(_WINDOWS_) );
  28.       WindowsInstall(InstallDirectory);
  29.       printf("\nPress any key to exit...");
  30.       getch();
  31.    }
  32.    return(EXIT_SUCCESS);
  33. }
  34.  
  35.  
  36. GiveInstructionsAndExit()  // Show some info about using this program
  37. {                          // and then exit this program. Do not return.
  38.    printf("Install.cmm - CEnvi installation procedure.  Execute with no parameters\n");
  39.    printf("              from the directory that contains CEnvi.exe and also contains\n");
  40.    printf("              the *.cmm and other sample CEnvi files, including Install.cmm\n");
  41.    if defined( _WINDOWS_ ) {
  42.       printf("\nPress any key to exit...");
  43.       getch();
  44.    }
  45.    exit(EXIT_FAILURE);
  46. }
  47.  
  48.  
  49. GetInstallDirectory(Executable)   // return current dir that is not on a floppy
  50. {
  51.    // current source directory from the name of the executable
  52.    CurDir = SplitFileName(FullPath(Executable)).dir;
  53.    assert( CurDir != NULL  &&  CurDir[0] != 0 );
  54.  
  55.    // check that current directory is not floppy A: or B:
  56.    if ( CurDir[0] == 'A'  ||  CurDir[0] == 'B' ) {
  57.       printf("\nCannot install CEnvi from a floppy.\n\a\n");
  58.       GiveInstructionsAndExit();
  59.    }
  60.  
  61.    // Have found the directory Install.cmm is in, and so check that
  62.    // CEnvi.exe is also in the same directory.
  63.    if ( NULL == Directory(strcat(strcpy(temp,CurDir),"CEnvi.exe")) ) {
  64.       printf("\nThis installation assumes that CEnvi.exe is in the same directory as\n");
  65.       printf("Install.cmm.  Could not find CEnvi.exe in the Install.cmm directory.\n\a\n");
  66.       GiveInstructionsAndExit();
  67.    }
  68.  
  69.    // remove extra backslash if at end of name (i.e., not root directory)
  70.    if ( strcmp(CurDir+1,":\\") )
  71.       CurDir[strlen(CurDir)-1] = 0;
  72.  
  73.    // return the result
  74.    return(CurDir);
  75. }
  76.  
  77.  
  78. Fatal(Format)  // like printf() but also beeps and exits program
  79. {
  80.    va_start(parameters,Format);
  81.    printf("\a\n");  // beep
  82.    vprintf(Format,parameters);
  83.    exit(EXIT_FAILURE);
  84. }
  85.  
  86.  
  87. GetYesOrNo()   // prompt for Yes or No, and return entered boolean TRUE
  88. {              // if YES else FALSE if NO.
  89.    printf(" (Y/N) ");
  90.    while( TRUE ) { // forever
  91.       key = toupper(getch());
  92.       if ( key != 'Y' && key != 'N' )
  93.          printf("\a");  // beep
  94.       else
  95.          break;
  96.    }
  97.    printf("%c\n",key);
  98.    return( key == 'Y' );
  99. }
  100.  
  101.  
  102. CopyFile(Source,Destination) // copy file from source to destination
  103. {
  104.    // open source and destination files
  105.    src = fopen(Source,"rb");
  106.    if ( src == NULL )
  107.       Fatal("Could not open source file \"%s\" for reading.",Source);
  108.    dest = fopen(Destination,"wb");
  109.    if ( dest == NULL )
  110.       Fatal("Could not open file \"%s\" for writing.",Destination);
  111.  
  112.    // read chunks from source, and copy to destination
  113.    #define  COPY_CHUNK_SIZE   500
  114.    while ( 0 != (size = fread(buf,COPY_CHUNK_SIZE,src)) ) {
  115.       fwrite(buf,size,dest);
  116.    }
  117.  
  118.    // close the files
  119.    fclose(dest);
  120.    fclose(src);
  121. }
  122.  
  123.  
  124. SkipWhitespace(str)
  125. {
  126.    while( isspace(str[0]) )
  127.       str++;
  128. }
  129.  
  130.  
  131. AlreadyInPath(Dir,Path) // search through path for this Dir, return True if found, else False
  132. {
  133.    len = strlen(Dir)
  134.    p = Path;
  135.    do {
  136.       if ( 0 == strnicmp(p,Dir,len)  && (p[len]==0 || p[len]==';') )
  137.          return(True)
  138.       p = strchr(p,';')
  139.    } while( p++ != NULL )
  140.    return(False)
  141. }
  142.  
  143.  
  144. AlterEVarLine(text,EVar,Dir)
  145.    // If this text is a line setting the EVar environment variable, and if
  146.    // Dir is not already in it, then add Dir.  Return TRUE if this is
  147.    // a line for EVar, and False if it is not.
  148. {
  149.    FoundEVar = FALSE; // assume this is not the line
  150.    // determine if text is of the type "EVAR=" or "set EVAR="
  151.    SkipWhitespace(c = text);
  152.    // if the next statement is "SET" then skip it
  153.    if ( !strncmpi(c,"SET",3) ) {
  154.       // Skip beyond the SET statement
  155.       c += 3;
  156.       SkipWhitespace(c);
  157.    }
  158.    // test if this next part is the variable name
  159.    EVarLen = strlen(EVar);
  160.    if ( !strncmpi(c,EVar,EVarLen) ) {
  161.       c += EVarLen;
  162.       if ( isspace(c[0])  ||  c[0] == '=' ) {
  163.          // THIS IS IT.  This line describes the EVar.
  164.          SkipWhitespace(c);
  165.          if ( c[0] == '=' ) {
  166.             c++;
  167.             SkipWhitespace(c);
  168.          }
  169.          FoundEVar = TRUE;
  170.          // If Dir is not already in this line then add Dir at the end.
  171.          // Check each dir as value between semicolons (;)
  172.          SkipWhitespace(c);
  173.          if ( !AlreadyInPath(Dir,c) ) {
  174.             // add this to the end of the existing path
  175.             if ( c[strlen(c)-1] != ';' )
  176.                strcat(c,";");
  177.             strcat(c,Dir);
  178.          }
  179.       }
  180.    }
  181.    return(FoundEVar);
  182. }
  183.  
  184. DosOrOS2Install(Dir,FileName,BackupFileName)
  185. {
  186.    // append the PATH statement so that it contains this directory, and
  187.    // also add the CMMPATH environment variable.  But give user a choice
  188.    // of whether to do this first; if they choose not to then tell them
  189.    // what they must do by hand.
  190.    printf("\nIf you choose, install will add the directory:\n\n");
  191.    printf("    \"%s\"\n\n",Dir);
  192.    printf("to the PATH environment variable in your %s file.  Install will\n",FileName);
  193.    printf("also add this directory to the CMMPATH environment variable in that\n");
  194.    printf("file.  If you select to make these changes to %s, then\n",FileName);
  195.    printf("install will backup the current version of %s to %s.\n",FileName,BackupFileName);
  196.    printf("\n\nShould these changes be made to %s?",FileName);
  197.    if ( GetYesOrNo() ) {
  198.       printf("Making changes to %s...",FileName);
  199.       // user chose to make automatic changes.  Do so now
  200.       CopyFile(FileName,BackupFileName);
  201.  
  202.       // will now read the backup file, and if any PATH or CMMPATH line is
  203.       // encountered then will alter it, else just copy line exactly.
  204.       src = fopen(BackupFileName,"rt");
  205.       if ( src == NULL )
  206.          Fatal("Could not open source file \"%s\" for reading.",BackupFileName);
  207.       dest = fopen(FileName,"wt");
  208.       if ( dest == NULL )
  209.          Fatal("Could not open source file \"%s\" for reading.",BackupFileName);
  210.       SetPath = SetCmmPath = False;
  211.       while ( NULL != (line = fgets(src)) ) {
  212.          // remove the pesky newline if there is one
  213.          if ( PeskyNewLine = (line[strlen(line)-1] == '\n') )
  214.             line[strlen(line)-1] = 0;
  215.          if ( AlterEVarLine(line,"PATH",Dir) )
  216.             SetPath = True;
  217.          if ( AlterEVarLine(line,"CMMPATH",Dir) )
  218.             SetCmmPath = True;
  219.          fputs(line,dest);
  220.          if ( PeskyNewLine )
  221.             fputs("\n",dest);
  222.       }
  223.       fclose(src);
  224.       // if haven't already written PATH or CMMPATH, then do so now
  225.       if ( !SetPath )
  226.          fprintf(dest,"\nSET PATH=%s\n",Dir);
  227.       if ( !SetCmmPath )
  228.          fprintf(dest,"\nSET CMMPATH=%s\n",Dir);
  229.       fclose(dest);
  230.       printf("\n%s has been altered.\n",FileName);
  231.       printf("Changes will take effect after you reboot.\n");
  232.    } else {
  233.       // user choose not to automatically install, so describe what the user
  234.       // needs to do by hand to make it work.
  235.       ScreenClear();
  236.       printf("\nThe PATH environment variable is used to find executable files, such as\n");
  237.       printf("CEnvi.exe, and batch files, such as those included in this CEnvi\n");
  238.       printf("unregistered shareware release.  The CMMPATH environment variable is\n");
  239.       printf("used by CEnvi to find common source code that may be included\n");
  240.       printf("in other CEnvi source files.\n\n");
  241.       printf("You chose not to have Install automatically alter %s.\n\n",FileName);
  242.       printf("You may wish to try Install.cmm again, or to make these modifications\n");
  243.       printf("to %s by hand.\n",FileName);
  244.    }
  245. }
  246.  
  247.  
  248. WindowsInstall(Dir)
  249. {
  250.    // If the user chooses, then add CMM Extensions and also add
  251.    // CMMPATH setting to WIN.INI.
  252.    printf("\nIf you choose, install will add the directory:\n\n");
  253.    printf("    \"%s\"\n\n",Dir);
  254.    printf("as a CMMPATH profile string in you WIN.INI file.  Install will also\n");
  255.    printf("also add the .CMM extension to WIN.INI so that you can execute any\n");
  256.    printf("*.CMM file simply by double-clicking on it.\n");
  257.    printf("\n\nShould these changes be made to WIN.INI?");
  258.    if ( GetYesOrNo() ) {
  259.       printf("Making changes to WIN.INI...");
  260.       // add extensions
  261.       sprintf(extension,"%s\\CENVI.EXE ^.CMM",Dir);
  262.       WriteProfileString("Extensions","CMM",extension);
  263.       // add CMMPATH
  264.       WriteProfileString("CEnvi","CMMPATH",Dir);
  265.       TellEveryoneThatWinIniHasChanged();
  266.       printf("\n");
  267.    } else {
  268.       // user choose not to automatically install, so describe what the user
  269.       // needs to do by hand to make it work.
  270.       ScreenClear();
  271.       printf("The .CMM extension in WIN.INI would allow you to execute CMM code simply by\n");
  272.       printf("double-clicking the file name.  The CMMPATH profile string allows CEnvi to\n");
  273.       printf("find Cmm source code or code library that may be in a different directory.\n");
  274.       printf("\n");
  275.       printf("You chose not to have Install automatically alter WIN.INI\n");
  276.       printf("\n");
  277.       printf("You may wish to make these changes by hand; if so, then add the line:\n");
  278.       printf("     CMM=%s\\CENVI.EXE ^.CMM\n",Dir);
  279.       printf("to the [Extensions] section of WIN.INI.  At the end of WIN.INI you may also\n");
  280.       printf("want to add these lines:\n");
  281.       printf("    [CEnvi]\n");
  282.       printf("    CMMPATH=%s\n",Dir);
  283.    }
  284. }
  285.  
  286.  
  287. WriteProfileString(ApplicationName,KeyName,KeyValue)
  288.    // This is a wrapper for the WriteProfileString in the Windows Kernel.
  289. {
  290.    DynamicLink("KERNEL","WRITEPROFILESTRING",SWORD16,PASCAL,
  291.                ApplicationName,KeyName,KeyValue);
  292. }
  293.  
  294.  
  295. TellEveryoneThatWinIniHasChanged()
  296. {
  297.    #define HWND_BROADCAST  0xFFFF
  298.    #define WM_WININICHANGE 0x001A
  299.    DynamicLink("USER","SENDMESSAGE",SWORD16,PASCAL,
  300.                HWND_BROADCAST,WM_WININICHANGE,0,0,0);
  301. }
  302.  
  303.